package com.whh.permission.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whh.permission.dao.IAssetDao;
import com.whh.permission.dao.IAssetHandlerDao;
import com.whh.permission.po.Asset;
import com.whh.permission.po.AssetScrap;
import com.whh.permission.po.AssetTake;
import com.whh.permission.service.IAssetHandlerService;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AssetHandlerServiceImpl implements IAssetHandlerService {
    @Resource
    private IAssetDao iAssetDao;
    @Resource
    private IAssetHandlerDao iAssetHandlerDao;

    //更改资产状态小工具
    public void assetStatusUtil(Integer aId,Integer status){
        Asset asset = new Asset();
        asset.setId(aId);
        asset.setStatus(status);
        iAssetDao.updateAsset(asset);
    }


    /**
     * 领取
     */
    @Override
    @Transactional
    public void takeAsset(Integer aId, Integer uId, Date createTime) {
        AssetTake assetTake = new AssetTake(aId,uId,createTime);
        assetStatusUtil(aId,1);
        iAssetHandlerDao.addAssetTake(assetTake);
    }

    @Override
    public PageInfo<AssetTake> queryAssetTakeByUId(int page, int pageSize, Integer uId) {
        PageHelper.startPage(page,pageSize);
        //查询领用信息
        List<AssetTake> list = iAssetHandlerDao.queryAssetTakeByUId(uId);
        
        PageInfo<AssetTake> assetTakePageInfo = new PageInfo<>(list);
        return assetTakePageInfo;
    }

    @Override
    @Transactional
    public void backAsset(Integer id, Date updateTime,Integer aId) {
        AssetTake assetTake = new AssetTake();
        assetTake.setId(id);
        assetTake.setStatus(1);
        assetTake.setUpdateTime(updateTime);
        iAssetHandlerDao.updateAssetTake(assetTake);
        assetStatusUtil(aId,2);
    }

    /**
     * 资产报废
     * @param assetScrap
     */
    @Override
    @Transactional
    public void ScrapAsset(AssetScrap assetScrap) {
        iAssetHandlerDao.addAssetScrap(assetScrap);
        assetStatusUtil(assetScrap.getAId(),3);
    }

    /**
     * 资产报废修好了
     * @param assetScrap
     */
    @Override
    @Transactional
    public void ScrapAssetOk(AssetScrap assetScrap) {
        System.out.println(assetScrap);
        iAssetHandlerDao.updateAssetScrap(assetScrap);
        assetStatusUtil(assetScrap.getAId(),2);
    }

    @Override
    public PageInfo<AssetScrap> queryAssetScrapAll(int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<AssetScrap> assetScraps = iAssetHandlerDao.queryAssetScrap();
        PageInfo<AssetScrap> assetScrapPageInfo = new PageInfo<>(assetScraps);
        return assetScrapPageInfo;
    }
}
